/**
 * Created by xiaoxiao on 2019/7/22.
 */
$(document).ready(function () {
    //八音分类
    var byJson = ajaxResult("get", false, "MbXtglBaseTableServlet.getPullData.do", {'tn': 'mb_gy_byfl'});
    loadSelect("#class_bayin", byJson.rows, 'id', 'name');

    var columnsUser = [
        {
            title: '序号', width: '5%', formatter: function (value, row, index) {
            return index + 1;
        }
        },
        {title: '乐器名称', field: 'title'},
        {title: '自称', field: 'title_self'},
        {title: '音标', field: 'phoneticsymbol'},
        {title: '名称解说词', field: 'titledescription'},
        {title: '霍萨分类', field: 'class_huosa_text'},
        {title: '八音分类', field: 'class_bayin_text'},
        {title: '民间分类', field: 'class_minjian_text'},
        {title: '出土地点', field: 'unearthedPlace'},
        {title: '年代', field: 'year_text'},
        {
            title: '操作', formatter: function (value, row, index) {
            if (row.pre_assignee == '') {
                return '<a class="tablehref" onclick="editData(' + row.task_id + ')">编辑</a><a class="tablehref" onclick="deleteData('+row.task_id+','+row.process_ins_id+')">删除</a>';
            } else {
                return '<a class="tablehref" onclick="checkData(' + row.task_id + ')">查看</a><a class="tablehref" onclick="deleteData('+row.task_id+','+row.process_ins_id+')">删除</a>';
            }
            // return '<a class="tablehref" onclick="itemUser(' + row.id + ')">提交</a>'
        }
        }
    ];
    var params = $("#submitForm").serializeObject();
    params["pageNumber"] = 1;
    params["limit"] = pageSize;
    params["offset"] = 0;
    createTable('#data-table', '/cnmuseum/bm/bmlist', 'id', columnsUser, params);
    $("#query_btn").click(function () {
        refreshTable("#data-table", "#submitForm");
    });
});
//删除新增保存的数据
function deleteData(task_id,process_id){
	bootbox.confirm("删除记录后不能恢复，确认删除当前记录吗？",function(r){
		if(r){
			var result=ajaxResult('POST',false,'/cnmuseum/bm/delete',{"taskId":task_id,"processId":process_id});
			if(result.status == 'OK'){
				alertFunc("记录删除成功！");			
				$('#data-table').bootstrapTable('refresh');
			}else{
				alertFunc(result["error"]);
			}
		}
	});
}
function refreshTable(tableid, formid) {
    var params = $(formid).serializeObject();
    params["pageNumber"] = 1;
    params["limit"] = pageSize;
    params["offset"] = 0;
    $(tableid).bootstrapTable('refresh', {query: params});
};
//添加保存
function save() {
    var params = $('#addDataForm').serializeObject();
    // if ($("#id_input").val() == '') {
    //     delete params["id"];
    // }
    //var result=ajaxResult('GET',false,'../../cjFlowServlet.savedata.do',params);
    var result = ajaxResult('POST', false, '/cnmuseum/bm/save', params);
    if (result.status == 'OK') {
        alertFunc('数据保存成功', function () {
            $("#addDataModal").modal("hide");
            $("#title_input").val("");
            $("#collection_code_input").val("");
            $("#id_input").val("");
            $('#data-table').bootstrapTable('refresh');
        });
    } else {
        alertFunc(result.data);
    }
}
//添加数据提交到下一步选择专家
function selectZj() {
    $("#addDataModal").modal('hide');
    var expertColumns = [
        {
            title: '选择', formatter: function (value, row, index) {
            return '<input name="expertId" type="checkbox" value="' + row.zdbh + '" />';
        }
        },
        {title: '专家名称', field: 'xm', width: 50},
        {title: '所在机构', field: 'szjg'},
        {title: '研究特长', field: 'yjtc'}
    ];
    createTable('#userInfo', '/cnmuseum/mbXtglZjkServlet.getZjkUser.do', 'zdbh', expertColumns, null, null);
    $("#nextExpertModal").modal('show');
}

//专家选择的提交
function submit(nextstep) {
    var assigneeuser = $("input[name='expertId']:checked").val();
    if (assigneeuser == '') {
        alertFunc('请先选择专家');
        return;
    }

    if (checkExpert(assigneeuser, dhbm_bjlr)) {
        alertFunc('所选择的专家有权限重复，请重新选择！');
        return;
    }

    var params = $('#addDataForm').serializeObject();
    params.assigneeuser = assigneeuser;
    // params.nextdesc = '专家录入';
    params.nextflow = 'lr';
    var result = ajaxResult('POST', false, '/cnmuseum/bm/submit', params);
    if (result.status == 'OK') {
        alertFunc('新增提交成功', function () {
            window.location.reload();
        });
    }
}
//新增数据采集保存后的编辑修改
function editData(task_id) {
    window.location.href = 'sjbmDetail.html?type=edit&task_id=' + task_id;
}
/*//新增数据采集保存后的编辑修改
function editData(task_id) {
    // window.location.href = 'sjbmDetail.html?type=edit&zdbh=' + id;
    var result = ajaxResult("get",false,"/cnmuseum/bm/getdetail",{"task_id":task_id});
    //var result = ajaxResult("get",false,"cnmuserum/mbgcxxservlet.getDetailById.do",{id:id});
    if(result.status=='OK'){
        $('#taskId_input').val(result.data.taskId);
        $('#processId_input').val(result.data.processId);
        $('#title_input').val(result.data.title);
        $('#collection_code_input').val(result.data.collection_code);
        $("#addDataModal").modal('show');
    }else{
        alertFunc("该条数据不存在！");
    }
}*/
//专家录入后提交给采集人，采集人提交审批页面
/*function checkData(id) {
    window.location.href = 'sjbmDetail.html?zdbh=' + id;
}*/

function showhsfl() {
    initSelectModel("Hs", "选择霍萨分类", "class_huosa", 1)
}

function showmjfl() {
    initSelectModel("Mj", "选择民间分类", "class_minjian", 1)
}

function checkData(task_id) {
    window.location.href = 'sjbmDetail.html?type=check&task_id=' + task_id;
}

